package com.zhxh.nzpos.sell;

import java.io.IOException;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.zhxh.nzpos.Constants;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.serialport.ReadCardNumber;

/**
 * 商品销售中的刷会员卡界面
 * 
 * @author wangzt
 * 
 */
public class SellShuakaActivity extends ReadCardNumber {

	protected SendingThread mSendingThread;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sell_shuaka);
		initViews();
	}

	private void initViews() {
		cardET = (EditText) findViewById(R.id.sell_card_et);

		Button okBtn = (Button) findViewById(R.id.shuaka_sell_ok_btn);
		okBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO 如果有卡号，则根据卡号取得会员积分

				Intent intent = new Intent();
				intent.putExtra(Constants.MEMBER_CARD, cardET.getText().toString());
				intent.setClass(getApplicationContext(), DealActivity.class);
				startActivity(intent);
				finish();
			}
		});

		Button cancelBtn = (Button) findViewById(R.id.shuaka_sell_cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});

		if (mSerialPort != null) {
			mSendingThread = new SendingThread();
			mSendingThread.start();
		}

	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

        if (mSerialPort != null && mSendingThread != null ) {
        	mSendingThread = new SendingThread();
			mSendingThread.start();
		}
		
	}
	

	protected class SendingThread extends Thread {
		@Override
		public void run() {
			while (!isInterrupted()) {
				synchronized (mByteReceivedBackSemaphore) {
					mByteReceivedBack = false;
					int size;

					try {
						if (mOutputStream != null) {
							mOutputStream.write(mValueToSend);

							// mOutputStream.write(mValueToSend3);

							// mOutputStream.write(mValueRead10);
						} else {
							return;
						}

						byte[] buffer = new byte[64];
						if (mInputStream == null)
							return;
						size = mInputStream.read(buffer);
						if (size > 0) {
							boolean flag = xunka(buffer, size);
							if (flag) {

								mOutputStream.write(mValueToCheck2);
								size = mInputStream.read(buffer);
								if (size > 0) {
									boolean yz = yanzheng(buffer, size);
									if (yz) {

										// 第2扇区第0块
										mOutputStream.write(mValueRead20);
										size = mInputStream.read(buffer);
										if (size > 0) {
											String dk = duka(buffer, size);
											if (dk != "") {
												System.out
														.println("读取第2扇区第1块成功!!!!!!!!!!!会员身份证信息前18位=="
																+ dk);
												idcard1 = dk;
											} else {
												// return;
											}
										}
										// 第2扇区第2块
										mOutputStream.write(mValueRead21);
										size = mInputStream.read(buffer);
										if (size > 0) {
											String dk = duka(buffer, size);
											if (dk != "") {
												System.out
														.println("读取第2扇区第2块成功!!!!!!!!!!!会员身份证信息后2位=="
																+ dk);
												idcard2 = dk;
											} else {
												// return;
											}
										}

									}
								}

								if (idcard1 != "" && idcard2 != ""
										&& card != "") {
									runOnUiThread(new Runnable() {
										public void run() {

											cardET.setText(card);
											// mTextViewLost.setText(mLost.toString());
										}
									});
									return;
								}

							} else {
								// return ;
							}
						}
					} catch (IOException e) {
						e.printStackTrace();
						return;
					}
				}
			}
		}
	}

	@Override
	protected void notifyActivity() {

	}

	@Override
	protected void onDestroy() {
		if (mSerialPort != null) {
			mSerialPort.close();
			if (mSendingThread != null)
				mSendingThread.interrupt();
		}
		super.onDestroy();
	}
}
